package com.itqf.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/16
 * @Time: 下午2:30
 */
public class MySalarySerializer extends JsonSerializer<Double> {

    /**
     *
     * @param aDouble  需要序列化的值
     * @param jsonGenerator  响应处理后的值
     * @param serializerProvider
     * @throws IOException
     */
    @Override
    public void serialize(Double aDouble, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        //18888.8888 ---> ￥18888.89 元
        //BigDecimal.valueOf(aDouble).setScale(2,BigDecimal.ROUND_HALF_UP)  保留两位小数
        String  newValue = "￥"+BigDecimal.valueOf(aDouble).setScale(2,BigDecimal.ROUND_HALF_UP)+" 元";
        jsonGenerator.writeString(newValue);
    }
}
